import { defineStore } from 'pinia'
import { authApi } from '@/api/auth'
import { menuApi } from '@/api/menu'
import { getToken, setToken, removeToken } from '@/utils/auth'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: getToken(),
    userInfo: null,
    permissions: [],
    menus: []
  }),
  
  getters: {
    isLoggedIn: (state) => !!state.token,
    hasPermission: (state) => (permission) => {
      return state.permissions.includes(permission)
    }
  },
  
  actions: {
    async login(loginData) {
      try {
        const response = await authApi.login(loginData)
        
        this.token = response.token
        this.userInfo = response.userInfo
        this.permissions = response.permissions || []
        this.menus = response.menus || []
        
        setToken(response.token)
        
        return response
      } catch (error) {
        throw error
      }
    },
    
    async logout() {
      try {
        await authApi.logout()
      } catch (error) {
        console.error('登出失败:', error)
      } finally {
        this.token = null
        this.userInfo = null
        this.permissions = []
        this.menus = []
        removeToken()
      }
    },
    
    async getCurrentUser() {
      try {
        const userInfo = await authApi.getCurrentUser()
        this.userInfo = userInfo
        return userInfo
      } catch (error) {
        console.error('获取用户信息失败:', error)
        throw error
      }
    },
    
    setUserInfo(userInfo) {
      this.userInfo = userInfo
    },
    
    setPermissions(permissions) {
      this.permissions = permissions
    },
    
    setMenus(menus) {
      this.menus = menus
    },
    
    async loadUserMenus() {
      try {
        if (this.userInfo?.id) {
          // 使用模拟数据，稍后替换为真实API
          const mockMenus = [
            {
              id: 1,
              menuName: '工作台',
              menuCode: 'dashboard',
              path: '/dashboard',
              icon: 'dashboard',
              visible: 1,
              children: []
            },
            {
              id: 2,
              menuName: '系统管理',
              menuCode: 'system',
              path: '/system',
              icon: 'system',
              visible: 1,
              children: [
                {
                  id: 3,
                  menuName: '用户管理',
                  menuCode: 'system-user',
                  path: '/system/user',
                  icon: 'user',
                  visible: 1,
                  children: []
                },
                {
                  id: 4,
                  menuName: '角色管理',
                  menuCode: 'system-role',
                  path: '/system/role',
                  icon: 'role',
                  visible: 1,
                  children: []
                },
                {
                  id: 5,
                  menuName: '权限管理',
                  menuCode: 'system-permission',
                  path: '/system/permission',
                  icon: 'permission',
                  visible: 1,
                  children: []
                },
                {
                  id: 6,
                  menuName: '菜单管理',
                  menuCode: 'system-menu',
                  path: '/system/menu',
                  icon: 'menu',
                  visible: 1,
                  children: []
                }
              ]
            },
            {
              id: 7,
              menuName: '系统监控',
              menuCode: 'monitor',
              path: '/monitor',
              icon: 'monitor',
              visible: 1,
              children: [
                {
                  id: 8,
                  menuName: '操作日志',
                  menuCode: 'monitor-log',
                  path: '/monitor/log',
                  icon: 'document',
                  visible: 1,
                  children: []
                }
              ]
            }
          ]
          
          this.menus = mockMenus
          return mockMenus
          
          // TODO: 后续替换为真实API调用
          // const menus = await menuApi.getUserMenus(this.userInfo.id)
          // this.menus = menus || []
          // return menus
        }
      } catch (error) {
        console.error('加载用户菜单失败:', error)
        this.menus = []
      }
    }
  }
})